<template>
  <div class="panel-tab__content">
    <div class="panel-tab__content--title">
      <span><i class="el-icon-menu" style="margin-right: 8px; color: #555555"></i>{{ $t('messageList') }}</span>
      <el-button size="mini" type="primary" icon="el-icon-plus" @click="openModel('message')">{{ $t('createNewMessage')
      }}</el-button>
    </div>
    <el-table :data="messageList" size="mini" border>
      <el-table-column type="index" :label="$t('serialNo')" width="60px" />
      <el-table-column :label="$t('messageId')" prop="id" max-width="300px" show-overflow-tooltip />
      <el-table-column :label="$t('messageName')" prop="name" max-width="300px" show-overflow-tooltip />
      <template slot="empty">
        <loading-state :outsidePosition="true" style="position: unset;" :echartShowStatus="3"></loading-state>
      </template>
    </el-table>
    <div class="panel-tab__content--title" style="padding-top: 8px; margin-top: 8px; border-top: 1px solid #eeeeee">
      <span><i class="el-icon-menu" style="margin-right: 8px; color: #555555"></i>{{ $t('signalList') }}</span>
      <el-button size="mini" type="primary" icon="el-icon-plus" @click="openModel('signal')">{{ $t('createNewSignal') }}
      </el-button>
    </div>
    <el-table :data="signalList" size="mini" border>
      <el-table-column type="index" :label="$t('serialNo')" width="60px" />
      <el-table-column :label="$t('signalId')" prop="id" max-width="300px" show-overflow-tooltip />
      <el-table-column :label="$t('signalName')" prop="name" max-width="300px" show-overflow-tooltip />
      <template slot="empty">
        <loading-state :outsidePosition="true" style="position: unset;" :echartShowStatus="3"></loading-state>
      </template>
    </el-table>

    <el-dialog
      :visible.sync="modelVisible"
      :title="modelConfig.title"
      :close-on-click-modal="false"
      width="400px"
      append-to-body
      destroy-on-close>
      <el-form :model="modelObjectForm" size="mini" label-width="auto" @submit.native.prevent>
        <el-form-item :label="modelConfig.idLabel">
          <el-input v-model="modelObjectForm.id" clearable />
        </el-form-item>
        <el-form-item :label="modelConfig.nameLabel">
          <el-input v-model="modelObjectForm.name" clearable />
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button size="mini" @click="modelVisible = false">{{ $t('cancel') }}</el-button>
        <el-button size="mini" type="primary" @click="addNewObject">{{ $t('save') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'SignalAndMassage',
  props: {
    businessObject: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      signalList: [],
      messageList: [],
      modelVisible: false,
      modelType: '',
      modelObjectForm: {}
    }
  },
  computed: {
    modelConfig () {
      if (this.modelType === 'message') {
        return { title: this.$t('createMessage'), idLabel: this.$t('messageId'), nameLabel: this.$t('messageName') }
      }
      return { title: this.$t('createSignal'), idLabel: this.$t('signalId'), nameLabel: this.$t('signalName') }
    }
  },
  watch: {
    businessObject: {
      immediate: true,
      handler (val) {
        if (val.flowElements) {
          this.$nextTick(() => {
            this.initDataList()
          })
        }
      }
    }
  },
  mounted () {
  },
  methods: {
    initDataList () {
      this.rootElements = window.bpmnInstances.modeler.getDefinitions().rootElements
      this.messageIdMap = {}
      this.signalIdMap = {}
      this.messageList = []
      this.signalList = []
      this.rootElements.forEach(el => {
        if (el.$type === 'bpmn:Message') {
          this.messageIdMap[el.id] = true
          this.messageList.push({ ...el })
        }
        if (el.$type === 'bpmn:Signal') {
          this.signalIdMap[el.id] = true
          this.signalList.push({ ...el })
        }
      })
    },
    openModel (type) {
      this.modelType = type
      this.modelObjectForm = {}
      this.modelVisible = true
    },
    // eslint-disable-next-line consistent-return
    addNewObject () {
      if (this.modelType === 'message') {
        if (this.messageIdMap[this.modelObjectForm.id]) {
          return this.$message.error(this.$t('errorMessageOfSignal'))
        }
        const messageRef = window.bpmnInstances.moddle.create('bpmn:Message', this.modelObjectForm)
        this.rootElements.push(messageRef)
      } else {
        if (this.signalIdMap[this.modelObjectForm.id]) {
          return this.$message.error(this.$t('errorMessageOfSignal'))
        }
        const signalRef = window.bpmnInstances.moddle.create('bpmn:Signal', this.modelObjectForm)
        this.rootElements.push(signalRef)
      }
      this.modelVisible = false
      this.initDataList()
    }
  }
}
</script>
